package vn.fpt.fsoft.controller;

import java.io.IOException;
import java.io.PrintWriter;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import vn.fpt.fsoft.dao.TrustDistrictDAO;
import vn.fpt.fsoft.dao.TrustRegionDAO;
import vn.fpt.fsoft.entity.TrustDistrict;
import vn.fpt.fsoft.entity.TrustRegion;

/**
 * Servlet implementation class AddTrustDistrict
 */
@WebServlet("/AddTrustDistrict")
public class AddTrustDistrict extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddTrustDistrict() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		String action=request.getParameter("action");
		if("getData".equalsIgnoreCase(action)){
			String strRes ="";
			TrustRegion tr = new TrustRegionDAO().getTrustRegionByKey(Integer.parseInt(request.getParameter("regionID")));
			strRes += "<input type='hidden' id ='regionID' value='" + tr.getTrustRegionID() + "' />"
			+"<input type='text' id='regionName' value='"+ tr.getName() + "' readonly='readonly'/>";
			out.print(strRes);
		}
		else if("add".equalsIgnoreCase(action)){			
			Integer regionId = Integer.parseInt(request.getParameter("regionID"));
			String regionName = request.getParameter("regionName");
			TrustRegion tr = new TrustRegion();
			tr.setTrustRegionID(regionId);
			tr.setName(regionName);
			
			String districtName = request.getParameter("name");
			String description = request.getParameter("description");
			TrustDistrict td = new TrustDistrict(districtName, description, tr, true);
			boolean success=TrustDistrictDAO.addTrustDistrict(td);	
			if(success){
				out.write("Adding this trust district success!");
			}else {
				out.write("Adding this trust district fails! Already have this district name!");
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);		
	}
/*	
	private String convertListToJson(List<TrustDistrict> list) {
		// declare a JSON object 
	    JsonObject responseDetailJson = new JsonObject();
	    // declare a JSON variable to use in loop
	    JsonObject jsonObject;
	    // declare a JSON array object to add JSON object
	    JsonArray jsonArray = new JsonArray();
	    //convert list to JSON string 
	    for(TrustDistrict tmp:list) {
	        jsonObject = new JsonObject();
	        jsonObject.addProperty("Name", tmp.getName());
	        jsonObject.addProperty("Description", tmp.getDescription());
	        jsonObject.addProperty("RegionName", tmp.getTrustRegion().getName());
	        jsonObject.addProperty("IsActive", tmp.getActive());
	        jsonArray.add(jsonObject);
	    }
	    // add JSON array into JSON object
	    responseDetailJson.add("TrustDistricts", jsonArray);
	    return String.valueOf(responseDetailJson);
	}
*/
}
